from nativePlugins.fuckAdvertising.fuckAdvertising import *

import os

def getTruePath():
    try:
        modpath = __file__
    except AttributeError:
        return None

    x = modpath.split("/")
    modpath_ = ""
    list(x).__delitem__(len(x) - 1)
    for item in x:
        if item == x[len(x) - 1]:
            continue
        else:
            modpath_ += item + "/"
    modpath = modpath_

    try:
        # Turn pyc files into py files if we can
        if modpath.endswith('.pyc') and os.path.exists(modpath[:-1]):
            modpath = modpath[:-1]
    except Exception as e:
        print(e)
    print(modpath)

    try:
        #return os.path.realpath(modpath)
        return modpath
    except Exception as e:
        return modpath

FILTERS = []

with open(getTruePath() + "adstrackingextended.hosts", "r", buffering=8192) as file:
    line = file.readline()
    while line:
        if not line.startswith("#") and line.__contains__(".") and not len(line) < 3:
            line = line.replace("\n", "")
            filter = line.split(" ")[1]
            FILTERS.append(filter)
        line = file.readline()

class AdsTrackingFilter(fuckAdvertisingFilter):
    def __init__(self):
        super()

    def allowRequest(self, netinfo):
        if FILTERS.__contains__(netinfo.getDomain()):
            return False
        return True

    def getName(self):
        return "fuckAdvertising: Lightswitch05's Ads and Tracking Extended"

AdsTrackingFilter_INSTANCE = AdsTrackingFilter()